//
//  NSMutableArray+Python.m
//  CommonTools
//
//  Created by caoyuan on 5/24/16.
//  Copyright © 2016 baidu. All rights reserved.
//

#import "NSMutableArray+Python.h"

@implementation NSMutableArray (Python)

- (NSMutableArray* (^)(id object))pyAppend
{
    return ^(id object) {
        //code
        
        if (object && [object isKindOfClass:[NSArray class]] ) {
            [self addObjectsFromArray:object];
        }else if  (object && ![object isKindOfClass:[NSArray class]]) {
            [self addObject:object];
        }
        else {
            [self addObject:[NSNull null]];
        }
        
        return self;
    } ;
}

- (NSMutableArray* (^)(NSInteger index))pyPopIndex
{
    return ^(NSInteger index) {
        //code
        if(index<[self count] && index >= 0) {
            [self removeObjectAtIndex:index];
        }
        return self;
    } ;
}

- (NSMutableArray* (^)(void))pyPop {
    return ^() {
        //code
        [self removeLastObject];
        
        return self;
    } ;
}

- (NSMutableArray* (^)(id object))pyRemove
{
    return ^(id object) {
        //code
        if (object && [self containsObject:object]) {
            [self removeObject:object];
        }
        return self;
    } ;
}

- (NSInteger (^)(id object))pyIndex {
    return ^(id object) {
        //code
        NSInteger index = -1;
        for (NSInteger i=0; i<[self count]; i++) {
            //            if ([self objectAtIndex:i] == object||[[self objectAtIndex:i] isEqualToString:object]) {
            //                return i;
            //            }
            if ([[self objectAtIndex:i] isEqual:object]) {
                return i;
            }
        }
        return index;
    } ;
}

- (id (^)(NSInteger))pyGetIndex {
    NSInteger length = [self count];
    return ^(NSInteger index) {
        if (index >= 0 && index <= length - 1) {
            return [self objectAtIndex:index];
        }
        else {
            return [self objectAtIndex:length+index];
        }
        
    };
}

- (NSMutableArray* (^)(NSInteger index, id object))pyInsert {
    return ^(NSInteger index, id object){
        
        if (index< 0) {
            index = 0;
        }
        if (index>[self count]) {
            index = [self count];
        }
        
        if (object) {
            [self insertObject:object atIndex:index];
        }
        else {
            [self insertObject:[NSNull null] atIndex:index];
        }
        return self;
    };
}

- (NSMutableArray* (^)(NSInteger start, NSInteger end))pyRange {
    return ^(NSInteger start, NSInteger end){
        
        if (start <= end) {
            if (start < 0) {
                start = 0;
            }
            if (end >[self count]) {
                end = [self count];
            }
            NSMutableArray *ary = [[NSMutableArray alloc] initWithArray:[self subarrayWithRange:NSMakeRange(start, end-start)]];
            
            return ary;
        }
        return self;
    };
}



- (NSMutableArray* (^)(NSInteger index, id object))pyReplace {
    return ^(NSInteger index, id object){
        
        if(index< 0 || index > [self count]) {
            return self;
        }
        
        if (object) {
            [self replaceObjectAtIndex:index withObject:object];
        }
        else {
            [self replaceObjectAtIndex:index withObject:[NSNull null]];
        }
        return self;
    };
    
}

- (NSMutableArray* (^)(void))pyReverse {
    return ^(void){
        
        NSMutableArray *ary = [[NSMutableArray alloc] initWithArray: [[self reverseObjectEnumerator] allObjects]];
        
        return ary;
    };
    
}

//NSComparisonResult changed by ENUM_CY_ARRAY_ORDER
- (NSMutableArray *(^)(ENUM_CY_ARRAY_ORDER))pyRorted {
    return ^(ENUM_CY_ARRAY_ORDER order) {
        NSMutableArray *arySorted = nil;
        if (order == ASCEND_ORDER) {
            arySorted = [[NSMutableArray alloc] initWithArray:[self sortedArrayUsingComparator:^NSComparisonResult(id elem1, id elem2) {
                return [elem1 compare:elem2];
            }]];
            
        }else{
            arySorted = [[NSMutableArray alloc] initWithArray:[self sortedArrayUsingComparator:^NSComparisonResult(id elem1, id elem2) {
                return [elem2 compare:elem1];
            }]];
            
        }
        
        return arySorted;
    };
}



- (void)forEachInArray: (void (^)(id eachElem, NSUInteger index))block {
    
    for (NSUInteger i=0; i<[self count]; i++) {
        block(self[i], i);
    }
    
}


- (void)forEachInArrayRange:(NSInteger)start
                    rangEnd:(NSInteger)end
             enumerateblock:(void (^)(id eachElem, NSInteger index))block {
    NSInteger len = [self count];
    if (start < 0) {
        start = 0;
    }
    if (end >= len) {
        end = len;
    }
    
    
    for (NSInteger i = start; i < end; i++) {
        block(self[i], i);
    }
    
}




@end




